$(function () {
  // 表单
  // semchuguoliuxue专题页 默认选中
  var ischecked = location.pathname.indexOf('semchuguoliuxue') || location.pathname.anchor.indexOf("UKniujianzhuanxiang") ||false
  $('#checkout-info').click(function () {
    $(this).attr(
      'src',
      ischecked ?
      ((location.pathname.indexOf('2020semlxgh') > 0 || location.pathname.indexOf('2020semyglx') > 0) ? 'http://xhd-lx-admin.oss-cn-hangzhou.aliyuncs.com/topic/2020semlxgh/images/arrow-default.png' : 'http://xhd-lx-admin.oss-cn-hangzhou.aliyuncs.com/topic/semdeguo/images/arrow-default.png') :
      'http://xhd-lx-admin.oss-cn-hangzhou.aliyuncs.com/topic/semdeguo/images/arrow-checked.png'
    )
    ischecked = !ischecked
  })
  var frontBaseUrl = 'http://liuxue.xhd.cn/'
  // 获取意向国家
  $.ajax({
    type: 'get',
    traditional: true,
    url: frontBaseUrl + 'common/dict/selectByTypeNames',
    data: {
      json: JSON.stringify([{
          name: '全站.留学意向地区',
          isGloble: 1,
        },
        {
          name: '全站.留学阶段',
          isGloble: 1,
        },
      ]),
      sid: 3,
    },
    dataType: 'json',
    success: function (res) {
      var data = res.data
      var len = data[0].length
      for (var i = 0; i < len; i++) {
        $('#intentionalArea').append(
          '<option value=' +
          data[0][i].value +
          '>' +
          data[0][i].name +
          '</option>'
        )
      }
      $('#intentionalArea').val(data[0][0].value)
    },
  })
  // 提交表单
  $('#submit').click(function () {
    var mobile = $('input[name=mobile]').val()
    var name = $('input[name=name]').val()
    if (validate(ischecked, name, mobile)) {
      var data = {
        _site_id_param: 3,
        mobile: $('input[name=mobile]').val(),
        name: $('input[name=name]').val() + '-' + location.href,
        intentionalArea: $('#intentionalArea').val(),
        businessName: '客户预约',
        moduleName: '免费留学评估',
        nv: 1,
      }
      $.get(frontBaseUrl + 'customer/insert', data, function (res) {
        if (res.code == 200) {
          showMessage('您的信息已提交，我们会尽快与您联系！', 1)
          $('input[name=mobile]').val('')
          $('input[name=name]').val('')
        } else {
          showMessage(res.msg, 0)
        }
      })
    }
  })
  // 提交请求前验证
  function validate(ischecked, name, mobile) {
    if (!ischecked) {
      showMessage('请勾选相关协议', 0)
      return false
    }
    if (name == '' || name.trim('').length === 0) {
      showMessage('请填写姓名', 0)
      return false
    }
    if (mobile == '') {
      showMessage('请填写手机号码！', 0)
      return false
    }
    if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(mobile)) {
      showMessage('请填写正确的手机号码！', 0)
      return false
    }
    return true
  }

  function showMessage(message, type) {
    let messageJQ = $("<div class='showMessage'>" + message + '</div>')
    if (type == 0) {
      messageJQ.addClass('showMessageError')
    } else if (type == 1) {
      messageJQ.addClass('showMessageSuccess')
    }
    /**先将原始隐藏，然后添加到页面，最后以600秒的速度下拉显示出来*/
    messageJQ.hide().appendTo('body').slideDown(600)
    /**3秒之后自动删除生成的元素*/
    window.setTimeout(function () {
      messageJQ.remove()
    }, 3000)
  }
})